#pragma once

#include <iostream>
#include <string>
#include <string.h>
#include <cstdlib>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <unistd.h>
#include "log.hpp"
#include "protocol.hpp"
using namespace std;

namespace client
{
    enum
    {
        Usage_ERR = 1,
        SOCKET_ERR,
        BIND_ERR,
        LISTEN_ERR,
        OPEN_ERR
    };
    class calclient
    {
    public:
        calclient(const string &ip, const uint16_t &port)
            : _serverip(ip), _serverport(port), _sock(-1)
        {
        }
        void initclient()
        {
            // 1、创建套接字
            _sock = socket(AF_INET, SOCK_STREAM, 0);
            if (_sock < 0)
            {
                cout << "create socket error" << endl;
                exit(2);
            }
        }
        void start()
        {
            // 5、发起连接
            struct sockaddr_in server;
            memset(&server, 0, sizeof(server));
            server.sin_family = AF_INET;
            server.sin_port = htons(_serverport);
            server.sin_addr.s_addr = inet_addr(_serverip.c_str());
            if (connect(_sock, (struct sockaddr *)&server, sizeof(server)) != 0)
            {
                cout << "socket connent error" << endl;
                exit(3);
            }
            else // 连接成功
            {
                string line;
                string inbuffer;
                while (1)
                {

                    cout << "mycal>>>";
                    getline(cin, line); // 输入1+1

                    Request req = ParseLine(line); // 对我们键盘输入的输做命令行解析
                    string content;
                    req.serialize(&content);
                    string send_string = enLength(content);
                    send(_sock, send_string.c_str(), send_string.size(), 0); // 发送暂时有bug
                    //send(_sock, send_string.c_str(), send_string.size()-3, 0); // 故意发送不完整的报文，服务端卡住了
                    string package, text;
                    if (!recvPackage(_sock, inbuffer, &package))//读取一个完整报文函数
                        continue;
                    //"content_len"\r\n"x op y"\r\n
                    if (!deLength(package, &text))
                        continue;
                    //"x op y"
                    Response resp;
                    resp.deserialize(text); // 反序列化
                    std::cout << "exitcode:" << resp.exitcode << std::endl;
                    std::cout << "result:" << resp.result << std::endl;
                }
            }
        }
        Request ParseLine(const string &line)//简易版状态机
        {
            //"11234+131231" "1/0"
            int status = 0; // 0、碰到操作符之前 1、碰到操作符 2、碰到操作符之后
            int i, cnt = line.size();
            string left, right;
            char op;
            while (i < cnt)
            {
                switch (status)
                {
                case 0:
                {
                    if (!isdigit(line[i]))
                        status = 1;
                    else
                        left.push_back(line[i++]);
                }
                break;
                case 1:
                {
                    op = line[i++];
                    status = 2;
                }
                break;
                case 2:
                {
                    right.push_back(line[i++]);
                }
                break;
                }
            }
            cout << stoi(left) << " " << stoi(right) << " " << op << endl;
            return Request(stoi(left), stoi(right), op);
        }
        ~calclient()
        {
            if (_sock >= 0)
                close(_sock);
        }

    private:
        int _sock;
        string _serverip;
        uint16_t _serverport;
    };
}